<?php

class FlatCategoryController extends Controller{


    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate($blockId)
    {
        $block = $this->loadBlock($blockId);

        if(!checkAccess('ownBlock', array('block'=>$block))){
            $this->accessDenied();
        }

        $category=new FlatCategory();

        if(isset($_POST['FlatCategory']))
        {
            $category->attributes=$_POST['FlatCategory'];


            if(!checkAccess('admin')){
             //   $block->block_id = $_POST['block'][]
            }

            $category->block_id = $blockId;

            if($category->save()){
                $this->redirect($this->linkToBlockUpdate($block));
            }
        }

        $this->render('create',array(
                'model'=>$category,
                'block'=>$block
        ));
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id)
    {
        $category=$this->loadModel($id);
        $block=$category->block;

        // permission check
        $this->accessDeniedIf(!checkAccess('ownBlock', array('block'=>$block)));

        if(isset($_POST['FlatCategory']))
        {
            $category->attributes=$_POST['FlatCategory'];
            if($category->save()){
                $this->redirect($this->linkToBlockUpdate($block));
            }
        }


        $this->render('update',array(
                'model'=>$category,
                'block'=>$block
        ));
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id)
    {

        $category=$this->loadModel($id);
        $block=$category->block;

        // permission check
        $this->accessDeniedIf(!checkAccess('ownBlock', array('block'=>$block)));

        $category->delete();


        if(!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : $this->linkToBlockUpdate($block));
    }

    protected function linkToBlockUpdate($block){

        return $this->createUrl('block/update', array('id'=>$block->id)) . '#flatCategories';

    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id)
    {
        $model=FlatCategory::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    public function loadBlock($id)
    {
        $model=Block::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

}